package com.ms.dsq_english.views;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.ref.WeakReference;

public class AutoPollRecyclerView extends RecyclerView
{
    private static final long TIME_AUTO_POLL = 100;
    AutoPollTask autoPollTask;
    //标示是否正在自动轮询
    private boolean running;
    //标示是否可以自动轮询,可在不需要的是否置false
    private boolean canRun;

    public AutoPollRecyclerView(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);

        autoPollTask = new AutoPollTask(this);
    }

    static class AutoPollTask implements Runnable
    {
        private final WeakReference<AutoPollRecyclerView> mReference;

        //使用弱引用持有外部类引用->防止内存泄漏
        public AutoPollTask(AutoPollRecyclerView reference)
        {
            this.mReference = new WeakReference<>(reference);
        }

        @Override
        public void run()
        {
            AutoPollRecyclerView recyclerView = mReference.get();

            if (recyclerView != null && recyclerView.running && recyclerView.canRun)
            {
                recyclerView.scrollBy(2, 2);//滑动x轴y轴的距离
                recyclerView.postDelayed(recyclerView.autoPollTask, recyclerView.TIME_AUTO_POLL);
            }
        }
    }

    //开启:如果正在运行,先停止->再开启
    public void start()
    {
        if (running) { stop(); }

        canRun = true;
        running = true;

        postDelayed(autoPollTask, TIME_AUTO_POLL);
    }

    public void stop()
    {
        running = false;
        removeCallbacks(autoPollTask);
    }
}
